/*禁用input标签*/
window.onload = function() {
    /* 设置数据表格区域的高度 */
    var height = window.innerHeight;
    var tab_head = document.getElementById("tab-head");
    var inputList = document.getElementsByTagName("input");
    for(var i = 0; i < inputList.length; i++) {
        inputList[i].setAttribute("readonly", "readonly");
    }
}
/*修改事件*/
function update(trid) {
    var tr = document.getElementById("info_tr"+trid); //德奥tr对象
    console.log("info_tr"+trid);
    var inputList = tr.getElementsByTagName("input"); //得到input对象数组
    var buts = tr.getElementsByTagName("button"); //得到button对象数组
    if(buts[0].innerHTML == "修改") {
        /*---启用当前行的input控件--*/
        for(var i = 0; i < inputList.length; i++) {
            inputList[i].removeAttribute("readonly");
            inputList[i].className = "update";
        }
        /*替换修改按钮*/
        buts[0].innerHTML = "保存";
        buts[1].style.display = "none";
    } else if(buts[0].innerHTML == "保存"){
        /*---启用当前行的input控件--*/
        for(var i = 0; i < inputList.length; i++) {
            inputList[i].setAttribute("readonly","readonly");
            inputList[i].removeAttribute("class");
        }
        /*替换修改按钮*/
        buts[0].innerHTML = "修改";
        buts[1].style.display = "";
        // $(buts[1]).removeAttr("display");
        // var form = tr.getElementsByTagName("form");
        var massage = "";
        console.log($(tr).find("input:eq(0)").val());
        $.post("../GetUsersServlet",{
            type:"update",
            sid:$(tr).find("input:eq(0)").val(),
            sname:$(tr).find("input:eq(1)").val(),
            sex:$(tr).find("input:eq(2)").val(),
            age:$(tr).find("input:eq(3)").val(),
            classes:$(tr).find("input:eq(4)").val(),
            sgroups:$(tr).find("input:eq(5)").val(),
            dorm:$(tr).find("input:eq(6)").val(),
            tel:$(tr).find("input:eq(7)").val()
        },"json").done(function(data){
            console.log("Servlet information:" + data + "\ttype:" + typeof(data));
            massage = '修改成功';
        }).fail(function(){
            massage = '出错了！！！';
            console.log("出错了 >< !!");
        }).always(function(){
            $("#windows_massage").html(massage);
            modal.style.display = "block";
        });
    }
}
/*删除事件*/
function remove(trid) {
    var massage = '';
    $.get("../GetUsersServlet",{
        type:"delete",
        sid:trid
    }).done(function(){
        $("#info_tr"+trid).remove();
        massage = '删除成功 ^ ^!';
    }).fail(function () {
        massage = '删除失败 ><!';
        console.log("remove def");
    }).always(function () {
        $("#windows_massage").html(massage);
        modal.style.display = "block";
    });
}
//关闭弹出
var modal = document.getElementById('div1');
// var btn = document.getElementById("button1");
var span = document.getElementById("span1");
// btn.onclick = function() {
//
// }
span.onclick = function() {
    modal.style.display = "none";
}
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
